package cn.liit.weixin.custommsg.util;

import java.util.Map;

import name.zbq.bush.utils.StringUtil;
import name.zbq.bush.utils.http.HttpConnect;

import com.alibaba.fastjson.JSONObject;

import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;
import cn.liit.weixin.core.basic.exception.WeixinException;
import cn.liit.weixin.custommsg.model.BaseMsg;
import cn.liit.weixin.custommsg.model.TextMsg;


public class MsgUtils {
	
	private static final String apiurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
	/**
	 * 发送客服消息
	 * @return
	 */
	public static Map<String,String> sendCustomMsg(BaseMsg baseMsg){
		String jsonStr = JSONObject.toJSONString(baseMsg);
		Map<String,String> result = HttpConnect.getInstance().doPostJson(apiurl+"?access_token="+AccessTokenSupport.getAccess_token(), jsonStr);
		return result;
	}
	/**
	 * 发送客服消息
	 * @return
	 */
	public static Map<String,String> sendCustomMsg(BaseMsg baseMsg, String accessToken){
		if(StringUtil.isEmpty(accessToken))
			throw new WeixinException("accessToken must be not null");
		if(baseMsg == null){
			throw new WeixinException("send message must be not null");
		}
		String jsonStr = JSONObject.toJSONString(baseMsg);
		Map<String,String> result = HttpConnect.getInstance().doPostJson(apiurl+"?access_token="+accessToken, jsonStr);
		return result;
	}
	public static void main(String[] args) {
		//okyoyuH6QsNDoWvd8j3gH9SQJJ7A
		//okyoyuDJe5l6p-s9l2ITsmtzHR6s
		Map<String,String> result = 
				MsgUtils.sendCustomMsg(new TextMsg("okyoyuH6QsNDoWvd8j3gH9SQJJ7A", "您好，在么"), 
				"4FdYehv7JqhDMNsooRsO6FbMPHo4f0dLJ_Kx3zYLlJIBSAM9IUdYmxYC0JJZnMhcin6C-yCsz_rRF9dxK--e6PCo872jtu2CKXijQULRZR8XRTrAGAFZB");
		System.out.println(result);
	}
}
